package com.blacktry.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.Admin;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.model.ExamManage;

import com.blacktry.entity.model.request.ExamRequest;
import com.blacktry.entity.model.request.ScoreRequest;
import com.blacktry.entity.model.request.StudentRequest;
import com.blacktry.service.ExamManageService;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考试管理表 前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-06-24
 */
@Api(value = "考试管理模块", tags = "考试管理接口")
@RestController
@RequestMapping("/exam")
public class ExamManageController {

    @Autowired
    private ExamManageService examManageService;

    @ApiOperation("添加考试")
    @PostMapping("/insert")
    public ApiResult<Integer> insert(@RequestBody ExamManage examManage){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.insert(examManage));
    }

    @ApiOperation("查看某状态中的考试")
    @PostMapping("/selectEnable/{flag}")
    public ApiResult<List<ExamManage>> selectEnable(@PathVariable Integer flag){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.selectEnable(flag));
    }

    @ApiOperation("更改考试的状态")
    @PutMapping("/updateEnable")
    public ApiResult<Integer> enable(@RequestParam("id") Integer id,@RequestParam("enable") Integer enable){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.updateEnable(id,enable));
    }

    @ApiOperation("查看所有考试")
    @GetMapping("/selectExam")
    public ApiResult<List<ExamManage>> selectExam(){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.selectExam());
    }

    @ApiOperation("分页查看所有考试")
    @PostMapping("/selectExam/{page}/{size}")
    public ApiResult<Page<ExamManage>> findAll(@RequestBody ExamManage examManage, @DefaultValue("1") @PathVariable("page") Integer page,@DefaultValue("6") @PathVariable("size") Integer size){
        System.out.println("分页查询所有试卷");
        Page<ExamManage> examManagePage = new Page<>(page,size);
        return ApiResultHandler.buildApiResult(200, "请求成功！", examManageService.selectExamPage(examManage,examManagePage));
    }

    @ApiOperation("分页查看学生所有线上考试")
    @PostMapping("/selectStudentOnlineExam/{page}/{size}")
    public ApiResult<Page<ExamManage>> selectStudentOnlineExam(@RequestBody ExamManage examManage, @DefaultValue("1") @PathVariable("page") Integer page,@DefaultValue("6") @PathVariable("size") Integer size){
        System.out.println("分页查询所有试卷");
        Page<ExamManage> examManagePage = new Page<>(page,size);
        return ApiResultHandler.buildApiResult(200, "请求成功！", examManageService.selectStudentOnlineExam(examManage,examManagePage));
    }

    @ApiOperation("添加/更新考试")
    @PostMapping("/updateOrInsertExam")
    public ApiResult<Integer> updateOrInsertExam(@RequestBody ExamManage examManage){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.updateOrInsertExam(examManage));
    }

    @ApiOperation("查询单个考试")
    @GetMapping("/selectById/{id}")
    public ApiResult<ExamManage> selectById(@PathVariable Integer id){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.selectExamOne(id));
    }

  //学生端查看考试
  @ApiOperation("学生端查看考试")
  @PostMapping("/studentSelectExam/{page}/{size}")
  public ApiResult<Page<ExamManage>> studentSelectExam(@DefaultValue("1")   @PathVariable("page") Integer page, @DefaultValue("6") @PathVariable("size") Integer size, @RequestBody ExamRequest examRequest, Principal principal){
      System.out.println("分页查询所有试卷");
      Page<ExamManage> Page = new Page<>(page,size);
      String userName=principal.getName();
      return ApiResultHandler.buildApiResult(200, "请求成功！", examManageService.selectExamPage2(userName,examRequest,Page));
  }

//    //教师端查看考试
//    @ApiOperation("教师端查看考试")
//    @PostMapping("/teacherSelectExam/{page}/{size}/{grade}/{source}/{createId}/{className}")
//    public ApiResult<Page<ExamManage>> teacherSelectExam( @DefaultValue("1")  @PathVariable("page") Integer page,@DefaultValue("6") @PathVariable("size") Integer size, @PathVariable("grade") String grade, @PathVariable("source") String source,@PathVariable("createId") String createId,@PathVariable("className") String className){
//        System.out.println("分页查询所有试卷");
//        Page<ExamManage> Page = new Page<>(page,size);
//        return ApiResultHandler.buildApiResult(200, "请求成功！", examManageService.teachSelectExamPage(createId,grade,className,source,Page));
//    }

    @ApiOperation("教师端查看考试")
    @PostMapping("/teacherSelectExam/{page}/{size}")
    public ApiResult<Page<ExamManage>> teacherSelectExam(@DefaultValue("1")  @PathVariable("page") Integer page ,@DefaultValue("6") @PathVariable("size") Integer size,@RequestBody ExamRequest examRequest){
        System.out.println("分页查询所有试卷");
        Page<ExamManage> Page = new Page<>(page,size);
        return ApiResultHandler.buildApiResult(200, "请求成功！", examManageService.teachSelectExamPage(examRequest,Page));
    }

    //查询每位考生的姓名，班级，考分，用时
    @OperLog(operModul = "考试管理-散点图查询",operType = OprLogConst.ADD,operDesc = "散点图查找功能")
    @ApiOperation("查询每位考生的姓名，班级，考分，用时")
    @GetMapping("/selectViewByExamId/{examId}")
    public ApiResult<List<ScoreRequest>> selectByExamId(@PathVariable String examId){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.selectByExamId(examId));
    }

    //根据考试id查询能参与的班级
    @ApiOperation("根据考试id查询能参与的班级")
    @GetMapping("/selectExamClass/{id}")
    public ApiResult<String> selectExamClass(@PathVariable String id){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.seclctExamClass(id));
    }


    //根据考试id查询能参与的学生
    @ApiOperation("根据考试id查询能参与的学生")
    @GetMapping("/selectExamStudent/{id}")
    public ApiResult<List<Admin>> seclctExamStudent(@PathVariable String id){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.seclctExamStudent(id));
    }

    //根据班级查询包含的学生
    @ApiOperation("根据班级查询包含的学生")
    @GetMapping("/selectStudentByClassName/{className}")
    public ApiResult<List<Admin>> selectStudentByClassName(@PathVariable String className){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.seclctExamStudent(className));
    }

    //查询考试及格不及格缺考尚未改卷的人数
    @ApiOperation("查询考试及格不及格缺考尚未改卷的人数")
    @GetMapping("/selectNumByExamId/{examId}")
    public ApiResult<Map<String,Integer>> selectNumByExamId(@PathVariable String examId){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.selectNumByExamId(examId));
    }


    //学生提交试卷and更改考试状态
    @ApiOperation("学生提交试卷and更改考试状态")
    @PutMapping("/updateExamState/{id}/{examId}/{username}/{enable}")
    public ApiResult updateExamState(@PathVariable("id") Integer id,@PathVariable("examId") String examId,@PathVariable("username") String userName,@PathVariable("enable") Boolean enable){
        this.examManageService.updateExamState(id,examId,userName,enable);
        return ApiResultHandler.buildApiResult(200,"成功");
    }

    //查询考试及格不及格缺考尚未改卷的学生信息
    @ApiOperation("查询考试及格不及格缺考尚未改卷的学生信息")
    @GetMapping("/selectStudentByExamId/{examId}")
    public ApiResult<Map<String,List<StudentRequest>>> selectStudentByExamId(@PathVariable String examId){
        return ApiResultHandler.buildApiResult(200,"成功",examManageService.selectStudentByExamId(examId));
    }
}

